<?php
/**
  * Blex Rewrite Demo
  * Short cut long URL, Express your URL
  * @author:iHao
  * @since:2007-11-25
  * @copyright http://ihao.net/blex
*/
DEFINE('BLEX', 'http://baidu.to.md/');
$b =& new blex();
$action=(!empty($_GET['action']))?strtolower(trim($_GET['action'])):'home';//方法名称，默认index
$url = (!empty($_POST['url']))?strtolower(trim($_POST['url'])):'';

$b->bHome($action,$url);

class blex {
	public function __construct() {
		//$this->db = mysql_connect('sql3.pandela.com', 'ihao_db', 'luohao');
		//mysql_select_db('ihao_db');
		$this->db = mysql_connect('localhost', 'root', '');
		mysql_select_db('blex');
		mysql_query("SET NAMES utf8");
		mysql_query("SET CHARACTER SET utf8");
		mysql_query("SET COLLATION_CONNECTION='utf8_general_ci'");
		header('content-type: text/html; charset=UTF-8');
		header('Cache-control: no-cache, must-revalidate');
	}
	public function __destruct() {
		mysql_close($this->db);
	}
	public function bHome($action = '',$url = ''){
		if ($action == 'home'){
			$this->bHeader();
			$this->bContent($url);
			$this->bFooter();
		} else {
			$this->bRewrite($action);
		}
	}
	public function bHeader($return = ''){
		echo('<!DOCTYPE html PUBLIC "-//W3C//DTD XTHML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml">');
		echo('<head>');
		echo('<meta http-equiv="content-type" content="text/html;charset=utf-8" />');
		echo('<meta http-equiv="cache-control" content="no-cache" />');
		if (strlen($return) > 0) {
			echo('<meta http-equiv="refresh" content="5;URL='.$return.'" />');
		}
		echo('<title>blex lab</title>');
		echo('</head><body>');
		$this->bCSS();
		echo('<h1><span>BLEX</span> - <span>B</span>lue ur<span>L</span> <span>EX</span>press ( express your URL )</h1>');
	}
	public function bCSS() {
		echo('<style>');
		echo('h1 span{color:blue;}');
		echo('body{border-top:5px solid blue;margin:0;padding:0;text-align:center;}');
		echo('h1{padding:0 10px;}');
		echo('h3{color:blue;}');
		echo('em{color:#999;}');
		echo('input.btn{border:2px solid red;-webkit-border-radius:5px;-moz-border-radius:5px;padding:2px;font-size:12px;color:#333;margin:0 5px;background-color:#FFF;}');
		echo('input.url{border:2px solid #0C0;-webkit-border-radius:5px;-moz-border-radius:5px;padding:3px;font-size:12px;width:500px;}');
		echo('p.footer{border-top:1px solid blue;padding:10px;}');
		echo('a{color:blue;padding:2px 5px;}a:hover{color:#0C0}');
		echo('</style>');
	}
	public function bContent($url = '') {
		if (strlen($url) > 0) {
			$this->bCreate($url);
		}
		$this->bSubmit();
	}
	public function bRewrite($action = ''){
		$sql = 'SELECT id, url FROM actions WHERE id = "'.$action.'" ORDER BY id DESC';
		$rs = mysql_query($sql);
		if (mysql_num_rows($rs) == 1) {
			$O = mysql_fetch_object($rs);
			mysql_free_result($rs);
			$this->bHeader($O->url);
			$this->bRecheck($action,$O->url);
			$this->bFooter();
		} else {
			$this->bHeader(BLEX);
			echo ('<span style="color:red;">this url is not active BLEX URL!<span>');
			echo ('<p>After 5 second redirected to <a href="'.BLEX.'">blex.cn</a></p>');
			$this->bFooter();
		}
	}
	public function bRecheck($action, $url) {
		echo('<p>After 5 second your browser will be redirected to <a href="'.$url.'">'.$url.'</a></p>');
		echo('<p>if you not sure <a href="'.$url.'">'.$url.'</a> is a safe url.<br />click <a href="'.BLEX.'">here</a> return <a href="'.BLEX.'">Blex.cn</a></p>');
	}
	public function bFooter() {
		echo('<p class="footer"><em><a href="http://blex.cn">Blex.cn</a>-Express Your URL | Author:<a href="http://ihao.net/">iHao.Net</a></em></p>');
		echo('</body></html>');
	}
	public function bSubmit(){
		?>
		<h3>Short cut your URL here:</h3>
		<form action="" method="POST">
		<input type="submit" value="SHORT IT" class="btn" />
		<input type="text" name="url" class="url" maxlength="1000" />
		</form>
		<strong style="">( <a style="color:red;">http://</a> ) <em> is required</em></strong> 
		<div style="margin:auto;width:600px;text-align:left;">
			<h5>For example:</h5>
			<p>Input:<em>http://member1.taobao.com/member/user-profile-caf6237e76025506887749d2a80f04e1.htm</em></p>
			<p>And BLEX will cut URL to:<em><a href="http://blex.cn/10">http://blex.cn/10</a></em></p>
			<p>Now is your time, have fun!</p>
		</div>
		<?php
	}
	public function bCreate($url){
		$remark = sha1($url);
		$sql = 'SELECT id FROM actions WHERE remark = "'.$remark.'" LIMIT 1';
		$rs = mysql_query($sql);
		if (mysql_num_rows($rs) == 1) {
			$O = mysql_fetch_object($rs);
			mysql_free_result($rs);
			echo('<h3>Bingo!</h3>');
			echo('New url: <a style="font-size:16px;" href="'.BLEX.$O->id.'">'.BLEX.$O->id.'</a><br />Old url: '.$url.'</p>');
		} else {
			$sql = 'INSERT INTO actions(action,url,remark) VALUES("ihao", "'.$url.'", "'.$remark.'")';
			mysql_query($sql);
			if (mysql_affected_rows($this->db) == 1) {
				echo('<h3>Bingo!</h3>');
				echo('New url: <a style="font-size:16px;" href="'.BLEX.mysql_insert_id().'">'.BLEX.mysql_insert_id().'</a><br />Old url: '.$url.'</p>');
			} else {
				echo('error!');
			}
		}
	}
}
?>